Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
"Unpacking" macro arguments

I have the following macro:

#define HEX 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

#define BITS 0x01

#define ADD_FLAGS(a, b, c, d, e, f, g, h) \
        a, b | BITS, c, d, e, f, g, h

I use this somewhere to create a byte array {ADD_FLAGS(HEX)}. This doesn't work. HEX is interpreted as a single argument to ADD_FLAGS and I get errors that there are too few arguments.

const myStructure Table[ENTRY_COUNT] =                   
{
   /* Entity Index 0 */
   {
      <some structure fields>,
      ADD_FLAGS(HEX),     // 8-byte array
      <more structure fields>
   }
}

I have also tried the following variants, but no success:

#define EXPAND(x) x
#define ADD_FL(a, b, c, d, e, f, g, h) \
        a, b | BITS, c, d, e, f, g, h
#define ADD_FLAGS(...) ADD_FL EXPAND((__VA_ARGS__))

##################### other variant
#define ADD_FLAGS(...) EXPAND(ADD_FL (__VA_ARGS__))

I am using C99 and three different compilers, so any answer has to be standards compliant, otherwise there is surely going to be a comiler that's not working (I am using armclang, ghs and tasking).

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Obligatory "avoid macros" aside, you just need to wrap ADD_FLAGS in another macro.

Rename it to something else, e.g. ADD_FLAGS_, and add

#define ADD_FLAGS(...) ADD_FLAGS_(__VA_ARGS__)

This works in GCC and Clang. In MSVC this works with the new preprocessor only (/Zc:preprocessor flag).

Not sure how to do it with the old MSVC preprocessor, but since it's notoriously buggy, I'd avoid supporting it if possible.

over 4 years ago · Santiago Trujillo Denunciar

0

I would suggest that you use macros sparingly. Here's an example snippet which does exactly what the macro is doing but it's a bit more reasonable in my opinion.

template<typename ... Ts>
constexpr auto add_flags(Ts && ... args){
     std::array flags{std::forward<Ts>(args)...};
     constexpr int bits = 0x01;
     // flags[some_idx] |= bits;
     return flags;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda